<?php
	//管理员类
	class admin
	{
		var $database = null;

		function admin()
		{
			$this->database = load( 'database' );
		}

		//返回管理员列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : 'account.id asc';
			$count = 'from account, role where account.role_id = role.id and account.status != 1';
			$list = 'select account.*, role.name as role_name ' . $count . ' order by ' . $order;
			$result = $this->database->split( $count, $list, $size, $page );
			return $result;
		}

		//返回具体管理员数据
		function get( $data )
		{
			$result = array();
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) ) $result = $this->database->unique( 'select * from account where id = ' . $data['id'] );
			return $result;
		}

		//修改具体管理员
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$check = array();

			if ( ! isset( $data['id'] ) || ! preg_match( '!^\d+$!', $data['id'] ) ) $check[] = '请传递用户ID';

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$this->database->set( 'account', $data, array( 'id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//添加管理员
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['username']['kind'] = '';
			$field['username']['type'] = 'text';
			$field['username']['min'] = '1';
			$field['username']['max'] = '32';
			$field['username']['info'] = '请输入用户名';

			$field['passwd']['kind'] = '';
			$field['passwd']['type'] = 'text';
			$field['passwd']['min'] = '1';
			$field['passwd']['max'] = '32';
			$field['passwd']['info'] = '请输入密码';

			$field['role_id']['kind'] = '';
			$field['role_id']['type'] = 'num';
			$field['role_id']['min'] = '1';
			$field['role_id']['max'] = '8';
			$field['role_id']['info'] = '请选择角色';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$account = load( 'biz.account' );
				$info = $account->info( $data['username'], 'name_by_local' );

				if ( ! isset( $info['userid'] ) )
				{
					$passport = $account->register( $data );

					if ( isset( $passport['status'] ) && $passport['status'] == 0 )
					{
						$id = $passport['id'];
						if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
						{
							$result['success'] = true;
							$result['data']['id'] = $id;
						}
						else
						{
							$result['message'][] = '执行创建失败';
						}
					}
					else
					{
						$result['message'][] = '注册失败.' . $passport['message'];
					}
				}
				else
				{
					$result['message'][] = '对不起，该用户名已存在';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//删除管理员
		function del( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$result = $this->database->command( 'update account set status=1 where id = ' . $data['id'] );
				$result['success'] = true;
			}
			return $result;
		}
	}
?>